using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Markup.Xaml;
using LayoutViewer.Views;
using System;

namespace LayoutViewer.Panels
{
    public partial class TitleBar : UserControl
    {
        public TitleBar()
        {
            this.InitializeComponent();
        }

        private void InitializeComponent()
        {
            AvaloniaXamlLoader.Load(this);
        }

        private void OnMinimizeTapped(object sender, RoutedEventArgs e)
        {
            MainWindow.Instance.WindowState = WindowState.Minimized;
        }

        private Window GetWindow(IControl control)
        {
            var parent = control.Parent;
            return parent != null ? parent is Window window ? window : this.GetWindow(parent) : throw new Exception("wwww");
        }

        private void OnCloseTapped(object sender, RoutedEventArgs e)
        {
            this.GetWindow(this).Close();
        }

        private void OnDragMoving(object sender, PointerPressedEventArgs e)
        {
            this.GetWindow(this).BeginMoveDrag(e);
        }
    }
}
